package com.yida.gwj.basis.service.impl;

import com.yida.common.core.utils.DateUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.mybatis.MybatisBatchUtils;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.basis.domain.GwjInitBusinessBuild;
import com.yida.gwj.basis.domain.GwjInitBusinessHouse;
import com.yida.gwj.basis.mapper.GwjInitBusinessBuildMapper;
import com.yida.gwj.basis.service.IGwjInitBusinessBuildService;
import com.yida.gwj.basis.service.IGwjInitBusinessHouseService;
import com.yida.gwj.basis.service.IGwjInitBusinessOwnerService;
import com.yida.gwj.common.domain.GwjBaseBusinessBuild;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务楼栋（基础数据管理模块）Service业务层处理
 *
 * @author lqq
 * @date 2022-11-15
 */
@Service
public class GwjInitBusinessBuildServiceImpl implements IGwjInitBusinessBuildService {

	@Resource
	private GwjInitBusinessBuildMapper businessBuildMapper;

	@Resource
	private IGwjInitBusinessHouseService businessHouseService;

	@Resource
	private IGwjInitBusinessOwnerService businessOwnerService;
	
	@Autowired
	private SqlSessionFactory sqlSessionFactory;

	/**
	 * 根据业务ID获取业务楼栋
	 *
	 * @param bizId 业务ID
	 * @return /
	 */
	@Override
	public List<GwjInitBusinessBuild> getListByBizId(Long bizId) {
		GwjInitBusinessBuild businessBuild = new GwjInitBusinessBuild();
		businessBuild.setBizId(bizId);
		return businessBuildMapper.selectList(businessBuild);
	}

	/**
	 * 根据业务小区ID获取业务楼栋
	 *
	 * @param bizCommunityId 业务小区ID
	 * @return /
	 */
	@Override
	public List<GwjInitBusinessBuild> getListByBizCommunityId(Long bizCommunityId) {
		GwjInitBusinessBuild businessBuild = new GwjInitBusinessBuild();
		businessBuild.setBizCommunityId(bizCommunityId);
		return businessBuildMapper.selectList(businessBuild);
	}

	/**
	 * 查询业务楼栋（基础数据管理模块）列表
	 *
	 * @param businessBuild 业务楼栋（基础数据管理模块）
	 * @return 业务楼栋（基础数据管理模块）集合
	 */
	@Override
	public List<GwjInitBusinessBuild> getList(GwjInitBusinessBuild businessBuild) {
		return businessBuildMapper.selectList(businessBuild);
	}

	/**
	 * 新增业务楼栋（基础数据管理模块）
	 *
	 * @param businessBuild 业务楼栋（基础数据管理模块）
	 * @return 结果
	 */
	@Override
	public int save(GwjInitBusinessBuild businessBuild) {
		businessBuild.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
		businessBuild.setCreateTime(DateUtils.getNowDate());
		return businessBuildMapper.insert(businessBuild);
	}

	/**
	 * 批量新增业务楼栋（基础数据管理模块）
	 *
	 * @param businessBuilds 业务楼栋集合（基础数据管理模块）
	 * @return 结果
	 */
	@Override
	public int saveBatch(List<GwjInitBusinessBuild> businessBuilds) {
		//return businessBuildMapper.insertBatch(businessBuilds);
	    return new MybatisBatchUtils().batchUpdateOrInsert(businessBuilds, GwjInitBusinessBuildMapper.class, (GwjInitBusinessBuild,businessBuildMapper)->businessBuildMapper.insert(GwjInitBusinessBuild), 10, sqlSessionFactory);
	    //return 
	}

	/**
	 * 修改业务楼栋（基础数据管理模块）
	 *
	 * @param businessBuild 业务楼栋（基础数据管理模块）
	 * @return 结果
	 */
	@Override
	public int updateById(GwjInitBusinessBuild businessBuild) {
		businessBuild.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
		businessBuild.setUpdateTime(DateUtils.getNowDate());
		return businessBuildMapper.updateById(businessBuild);
	}

	/**
	 * 批量修改业务楼栋（基础数据管理模块）
	 *
	 * @param businessBuilds 业务楼栋（基础数据管理模块）
	 * @return 结果
	 */
	@Override
	public int updateBatch(List<GwjInitBusinessBuild> businessBuilds) {
		return businessBuildMapper.updateBatch(businessBuilds);
	}

	/**
	 * 删除楼栋及房屋业主
	 *
	 * @param businessBuilds 楼栋集合
	 * @param bizId          业务ID
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteByList(List<GwjInitBusinessBuild> businessBuilds, Long bizId) {
		//删除楼栋
		Long[] ids = businessBuilds.stream().map(GwjBaseBusinessBuild::getBizBuildId).toArray(Long[]::new);
		businessBuildMapper.deleteByIds(ids);
		List<String> delHouseCodes = new ArrayList<String>();
		List<String> delBuildCodes = new ArrayList<String>();
		businessBuilds.forEach(build -> {
			List<GwjInitBusinessHouse> houses = businessHouseService.getList(build.getBizId(), build.getBuildCode());
			if (StringUtils.isNotEmpty(houses)) {
				houses.forEach(house -> {
					delHouseCodes.add(house.getHouseCode());
				});
				delBuildCodes.add(build.getBuildCode());
			}
		});
		//删除房屋
		if (StringUtils.isNotEmpty(delBuildCodes)){
			businessHouseService.deleteByBuildCodesAndBizId(delBuildCodes, bizId);
		}
		//删除业主
		if (StringUtils.isNotEmpty(delHouseCodes)){
			businessOwnerService.deleteByHouseCodesAndBizId(delHouseCodes, bizId);
		}
	}

	/**
	 * 根据业务ID删除楼栋
	 *
	 * @param bizId 业务ID
	 * @return /
	 */
	@Override
	public int deleteByBizId(Long bizId) {
		return businessBuildMapper.deleteByBizId(bizId);
	}

}
